//
//  CommandSetsManager.m
//  Yuejia
//
//  Created by 陈家将 on 17/6/27.
//  Copyright (c) 2017年 chenjiajiang. All rights reserved.
//

#import "CommandSetsManager.h"
#import "MasterViewController.h"
#import "DetailViewController.h"


@implementation CommandSetsManager

/** 执行App初始化设置 */
+ (void)executeConmandSetWithApplication:(UIApplication *)application AppDelegate:(AppDelegate *)delegate {
    
    delegate.window = [[UIWindow alloc] initWithFrame:SCREEN_BOUNDS];
    [self executeCreatWindowWithApplication:application AppDelegate:delegate];// 创建窗口
//    [self executeSettingNotificationWithApplication:application];// 注册APNs
    [self executeSettingKeyboardManager];// 全局键盘设定
//    [self executeSettingShareSDK];// 第三方分享信息初始化
//    [self executeSettingPaypal];// Paypal支付信息初始化
//    [self executeSettingWeChatPay];// Paypal支付信息初始化
//    [self getlastVersionFromAppStore]; // 获取商店App版本
//    [self executeSettingAlibaba]; // 初始化百川电商SDK
    
    
    
#ifdef DEBUG
#else
//    [self executeSettingFirebase];// Firebase初始化
//    [self ExceptionHandling];// 异常捕获
#endif
}
/*
+ (void)executeSettingAlibaba {
 
    // 百川平台基础SDK初始化，加载并初始化各个业务能力插件
    [[AlibcTradeSDK sharedInstance] asyncInitWithSuccess:^{
        
        DBLog(@"Init success");
        
    } failure:^(NSError *error) {
        // 初始化失败,退出授权
        [[ALBBSDK sharedInstance] logout];
        DBLog(@"Init failed: %@", error.description);
        
    }];
    
    NSString *boundVersion  = kAPP_VERSION;
    if (![boundVersion isKindOfClass:[NSString class]]) {
        NSString *saveVersion  = [[NSUserDefaults standardUserDefaults] objectForKey:@"versionStr"];
        if (!saveVersion || ![saveVersion isKindOfClass:[NSString class]] || !saveVersion.length ) {
            boundVersion = @"0.0.0.1";
        } else {
            boundVersion = saveVersion;
        }
    }
    
    [[AlibcTradeSDK sharedInstance] setIsvVersion:kAPP_VERSION];
#ifdef DEBUG
    // 开发阶段打开日志开关，方便排查错误信息
    //默认调试模式打开日志,release关闭,可以不调用下面的函数
    [[AlibcTradeSDK sharedInstance] setDebugLogOpen:YES];
#else
    [[AlibcTradeSDK sharedInstance] setDebugLogOpen:NO];
#endif

    
//    // 配置全局的淘客参数
//    //如果没有阿里妈妈的淘客账号,setTaokeParams函数不需要调用
    AlibcTradeTaokeParams *taokeParams = [[AlibcTradeTaokeParams alloc] init];
    taokeParams.pid = AlimamaTrandePid; //mm_XXXXX为你自己申请的阿里妈妈淘客pid
    [[AlibcTradeSDK sharedInstance] setTaokeParams:taokeParams];
    
    //设置全局的app标识，在电商模块里等同于isv_code
    //没有申请过isv_code的接入方,默认不需要调用该函数
//    [[AlibcTradeSDK sharedInstance] setISVCode:@"com.cnstorm.CNstorm"];
    
    // 设置使用支付宝支付
    [[AlibcTradeSDK sharedInstance] setShouldUseAlipayNative:YES];
    // 设置全局配置，是否强制使用h5,
//    [[AlibcTradeSDK sharedInstance] setIsForceH5:NO];
    
}

+ (void) getlastVersionFromAppStore {
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:@"https://itunes.apple.com/lookup?id=914402588" parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
        if (![responseObject isKindOfClass:[NSDictionary class]]) {
            return ;
        }
        
        if (1 != [responseObject[@"resultCount"] intValue]) {
            return;
        }
        NSArray *array = responseObject[@"results"];
        NSDictionary *dict = [array lastObject];
        NSString *versionInAppStore = dict[@"version"];
//        DBLog(@"App Store当前版本为：%@", dict[@"version"]);
        NSString *releaseNotes = dict[@"releaseNotes"];
//        DBLog(@"App Store当前版本更新内容为：%@", releaseNotes);
        NSString *trackViewUrl = dict[@"trackViewUrl"];
//        DBLog(@"App Store当前版本更新链接为：%@", trackViewUrl);
        
        if (releaseNotes.length) {
            [[NSUserDefaults standardUserDefaults] setObject:releaseNotes forKey:@"AppReleaseNotes"];
        }
        
        if (trackViewUrl.length) {
            [[NSUserDefaults standardUserDefaults] setObject:trackViewUrl forKey:@"AppTrackViewUrl"];
        }
        
        // 记录应用商店中的版本
        [[NSUserDefaults standardUserDefaults] setObject:versionInAppStore forKey:@"VersionInAppStore"];
        
    } failure:nil];
}
*/
/** 异常捕获 */
/*
+ (void)ExceptionHandling {
    NSSetUncaughtExceptionHandler (&sendExceptionReasonToFireBase);
}
 */
/*
// 抛异常的回调函数
void sendExceptionReasonToFireBase(NSException *exception) {
    NSArray *arr = [exception callStackSymbols];//得到当前调用栈信息
    NSString *reason = [exception reason];//非常重要，就是崩溃的原因
    NSString *name = [exception name];//异常类型
    
    DBLog(@"%@",[[[UIApplication sharedApplication] activityViewController] class]);
    
    DBLog(@"exception type : %@ \n crash reason : %@ \n call stack info : %@", name, reason, arr);
    FIRCrashLog(@"exception type : %@ \n FromVc : %@ \n crash reason : %@ \n call stack info : %@", name, [[[UIApplication sharedApplication] activityViewController] class], reason, arr);
}
*/
/** 初始化Firebase */
/*
+ (void)executeSettingFirebase {
    [FIRApp configure]; // 配置Firebase
}

+ (void)executeSettingWeChatPay {
    [WXApi registerApp:WX_AppID withDescription:@"CNstorm_iOS"];
}
*/
/** 初始化Paypal参数 */
/*
+ (void)executeSettingPaypal {
    
#ifdef DEBUG
    [PayPalMobile initializeWithClientIdsForEnvironments:@{PayPalEnvironmentProduction : @"ARfVVRCkAT7uNlLwgx10nPQL-93eVGS-lAF31Qfv8tpW01IxBeJf0iupGEt0",                                                                                                                 PayPalEnvironmentSandbox : @"AUOf9RD5NrvSV_n54YiNwKm0RUndJ3KC9ZV7o2kekIAHeGDwcNOZ7D47nnRb"}];// 沙箱测试
#else
    [PayPalMobile initializeWithClientIdsForEnvironments:@{
                                                           PayPalEnvironmentProduction : @"AcmbLBAMsXbenzSdHNE9GYwI-KGMQK4flFyOoYTru7VP1k82QNaFktwqWxEb",PayPalEnvironmentSandbox : @"AcoABhBW5a9zaCbzpg9SdeOiBlJdEy-uXBrpaAI5KwkXogGACXrL0R47DSvi"
                                                           }];// 正式环境
#endif
    
}
*/
/** 执行创建window */
+ (void)executeCreatWindowWithApplication:(UIApplication *)application AppDelegate:(AppDelegate *)delegate {
    
    delegate.window.backgroundColor = [UIColor whiteColor];
    UISplitViewController *splitViewController = [[UISplitViewController alloc] init];
    
    MasterViewController *masterVc = [[MasterViewController alloc] init];
    UINavigationController *masterNaviVc = [[UINavigationController alloc] initWithRootViewController:masterVc];
    masterNaviVc.topViewController.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem;
    
    DetailViewController *detailVc = [[DetailViewController alloc] init];
    UINavigationController *detailNaviVc = [[UINavigationController alloc] initWithRootViewController:detailVc];
    
    masterVc.delegate = detailVc;
    splitViewController.viewControllers = @[masterNaviVc,detailNaviVc];

    splitViewController.delegate = delegate;
    splitViewController.maximumPrimaryColumnWidth = SCREEN_WIDTH;
    splitViewController.preferredPrimaryColumnWidthFraction = 0.7;
    
    delegate.window.rootViewController = splitViewController;
    [delegate.window makeKeyAndVisible];
}

/** 执行注册通知 */
/*
+ (void)executeSettingNotificationWithApplication:(UIApplication *)application {
    
    UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
    
    UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:type categories:nil];
    
    [application registerUserNotificationSettings:setting];
    [application registerForRemoteNotifications];
#ifdef DEBUG
    
#else
    [APService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                   UIUserNotificationTypeSound |
                                                   UIUserNotificationTypeAlert)
                                       categories:nil];
#endif
}
*/
/** 执行全局键盘设定 */

+ (void)executeSettingKeyboardManager {
    // 全局键盘设定
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
}


/** 执行ShareSDK设定 */

+ (void)executeSettingShareSDK {
    
}

@end
